package game.graphics;

import java.util.ArrayList;

import game.structures.Point;

public class LoopbackAnimation implements Animation {

	private float currentTime;
	private float currentSpeed;
	
	private int currentFrame;
	private int endFrame;
	private String name;
	private ArrayList<Frame> frames;
	private boolean ending;
	
	public LoopbackAnimation(String name, ArrayList<Frame> frames, int endFrame) {

		this.frames = frames;
		this.endFrame = endFrame;
		this.name = name;
		
		ending = false;
		currentTime = 0f;
		currentSpeed = 1f;
		
		currentFrame = 0;
	}
	
	public String getName() {
		return name;
	}
	
	public boolean isEnded() {
		if(currentFrame == -1)
			return true;
		return false;
	}
	
	public void beginExit() {
		if(!ending) {
			ending = true;
			currentFrame = endFrame;
		}
	}
	
	public void setSpeed(float speed) {
		currentSpeed = speed;
	}
	
	public void reset() {
		currentTime = 0;
		currentFrame = 0;
		ending = false;
	}

	public void update(int delta) {
		if(currentFrame != -1) {
			currentTime += currentSpeed * delta;
			if (frames.get(currentFrame).getFrameEnd() <= currentTime) {
				currentFrame = frames.get(currentFrame).getNext();
				currentTime = 0;
			}
		}
	}

	public void draw(float x, float y) {
		frames.get(currentFrame).draw(x, y);
	}

	public void draw(float x, float y, float xSkew, float ySkew, float alpha, float rot) {
		frames.get(currentFrame).setAlpha(alpha);
		frames.get(currentFrame).setRotation(rot);
		
		float width = 	frames.get(currentFrame).getWidth();
		float height =  frames.get(currentFrame).getHeight();
		
		frames.get(currentFrame).draw(
									x, 
									y, 
									xSkew, 
									ySkew);
	}
	
}
